package com.cinsea.ext.cjspx;

import com.cinsea.common.ActionType;
import com.cinsea.common.BasePX;
import com.cinsea.common.Env;
import com.cinsea.common.JdbcDao;
import com.cinsea.exception.DataOperationException;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;
import com.cinsea.workflow.model.Wfnode;

import java.util.List;
import java.util.Map;


public class HrmPX extends BasePX {
    @Override
    public void after(Map context) {
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        String actiontype = StringUtil.null2String(context.get("actiontype"));
        Wfnode node = (Wfnode) context.get("wfnode");
        String processid = (String) context.get("processid");
        //node:值班记录->人力资源部确认 48f5f3ec398ee43b01398f03affb0020
        if (actiontype.equals(ActionType.SUBMIT_WFPROCESS) && node != null && node.getId().equals("48f5f3ec398ee43b01398f03affb0020")) {//值班记录->人力资源部确认后加一天到人员表中的调休天数和剩余调休天数上
            String sql = "select dutyname,dutydate from ut_dutyapp where processid=?";
            List<Map> l = jdbcDao.queryForList(sql, new Object[]{processid});
            Map data = l.get(0);
            String dutyname = (String) data.get("dutyname");
            String dutydate = (String) data.get("dutydate");
            String updatesql = "update cs_user set sumtxdays=isnull(sumtxdays,0)+1,usetxdays=isnull(usetxdays,0)+1 where id=?";
            jdbcDao.update(updatesql, new Object[]{dutyname});
        }
        //node:请假申请->申请人 40895a0035efe70f0135eff58e5b0030
        //公休假 40895a0035efe70f0135efeddae70005 调休 40895a0035efe70f0135efeea86e0008
        if (actiontype.equals(ActionType.SUBMIT_WFPROCESS) && node != null && node.getId().equals("40895a0035efe70f0135eff58e5b0030")) {//请假申请->申请人提交时要判断请假天数是否大于年假表中的剩余天数
            String sql = "select applicator,leavedays,startdate,enddate,type1 from ut_leaveapp where processid=?";
            List<Map> l = jdbcDao.queryForList(sql, new Object[]{processid});
            Map data = l.get(0);
            String applicator = (String) data.get("applicator");
            String startdate = (String) data.get("startdate");
            String type1 = (String) data.get("type1");
            float leavedays = NumberUtil.string2Float(String.valueOf(data.get("leavedays")), 0);
            if (type1.equals("40895a0035efe70f0135efeddae70005")) {//公休假
                sql = "select sum(checkdays-isnull(usedays,0)) as canuse  from ut_vacationinfo where name=? and startdate<=? and enddate>=?";//已使用天数 usedays
                l = jdbcDao.queryForList(sql, new Object[]{applicator, startdate, startdate});
                data = l.get(0);
                float canusedays = NumberUtil.string2Float(String.valueOf(data.get("canuse")), 0);
                System.out.println("sql:"+"select sum(checkdays-isnull(usedays,0)) as canuse  from ut_vacationinfo where name='"+applicator+"' and startdate<='"+startdate+"' and enddate>='"+startdate+"'");
                System.out.println("canusedays:"+String.valueOf(data.get("canuse")));
                if (canusedays <= 0) {
                    throw new DataOperationException("可用天数为零");
                }
                if (leavedays > canusedays) {
                    throw new DataOperationException("可用天数不足(可用:" + canusedays + "天)");
                }

            } else if (type1.equals("40895a0035efe70f0135efeea86e0008")) {//调休
                sql = "select usetxdays from cs_user where id=?";
                l = jdbcDao.queryForList(sql, new Object[]{applicator});
                float usetxdays = NumberUtil.string2Float(String.valueOf(l.get(0).get("usetxdays")), 0);
                if (usetxdays <= 0) {
                    throw new DataOperationException("可用调休天数为零");
                }
                if (leavedays > usetxdays) {
                    throw new DataOperationException("可用调休天数不足(可用:" + usetxdays + "天)");
                }

            }

        }
        //node:请假申请->人力资源部登记 40895a0035efe70f0135eff58e6b0033
        if (actiontype.equals(ActionType.SUBMIT_WFPROCESS) && node != null && node.getId().equals("40895a0035efe70f0135eff58e6b0033")) {
            String sql = "select applicator,leavedays,startdate,enddate,type1 from ut_leaveapp where processid=?";
            List<Map> l = jdbcDao.queryForList(sql, new Object[]{processid});
            Map data = l.get(0);
            String applicator = (String) data.get("applicator");
            String startdate = (String) data.get("startdate");
            String type1 = (String) data.get("type1");
            float leavedays = NumberUtil.string2Float(String.valueOf(data.get("leavedays")), 0);
            if (type1.equals("40895a0035efe70f0135efeddae70005")) {//公休假

                //扣除操作
                sql = "select id,(checkdays-isnull(usedays,0)) as canuse from ut_vacationinfo where name=? and startdate<=? and enddate>=? order by enddate";
                l = jdbcDao.queryForList(sql, new Object[]{applicator, startdate, startdate});
                String id0 = (String) l.get(0).get("id");
                float canusedays0 = NumberUtil.string2Float(String.valueOf(l.get(0).get("canuse")), 0);

                if (leavedays <= canusedays0) {
                    String updatesql = "update ut_vacationinfo set usedays=isnull(usedays,0)+" + leavedays + " where id=?";
                    jdbcDao.update(updatesql, new Object[]{id0});
                } else {
                    String updatesql = "update ut_vacationinfo set usedays=checkdays where id=?";//先扣光延期的
                    jdbcDao.update(updatesql, new Object[]{id0});
                    updatesql = "update ut_vacationinfo set usedays=isnull(usedays,0)+" + (leavedays - canusedays0) + " where id=?";//再扣本年的
                    jdbcDao.update(updatesql, new Object[]{l.get(1).get("id")});
                }


            } else if (type1.equals("40895a0035efe70f0135efeea86e0008")) {//调休，扣除员工表中的可用调休天数usetxdays
                //扣除操作
                String updatesql = "update cs_user set usetxdays=usetxdays-" + leavedays + " where id=?";
                jdbcDao.update(updatesql, new Object[]{applicator});
            }

        }
    }
}
